package com.ifly.model;

import com.ifly.exception.BizException;
import com.ifly.utils.DateUtils;

/**
 * 订单实体类（企业规范：必须通过build()创建，且包含金额校验）
 */
public class Order {
    private static final double MIN_AMOUNT = 0.01;

    private String orderId;
    private double amount;
    private OrderStatus status;
    private String createTime;

    // 企业规范：禁止直接构造，必须用build()
    private Order() {}

    /**
     * 创建订单（企业规范：金额必须≥0.01）
     * @param orderId 订单ID（格式：ORDER_前缀+8位数字）
     * @param amount 订单金额（单位：元）
     * @throws BizException 如果金额无效抛出 INVALID_AMOUNT
     */
    public static Order build(String orderId, double amount) throws BizException {
        if (amount < MIN_AMOUNT) {
            throw BizException.of("INVALID_AMOUNT", "金额不能小于" + MIN_AMOUNT);
        }

        Order order = new Order();
        order.orderId = orderId;
        order.amount = amount;
        order.status = OrderStatus.CREATED;
        order.createTime = DateUtils.getCurrentTime(); // 企业规范：必须用DateUtils
        return order;
    }

    // 企业规范：所有Getter必须加注释
    /** 获取订单状态 */
    public OrderStatus getStatus() {
        return status;
    }

    /** 企业规范：状态变更必须通过此方法 */
    public void setStatus(OrderStatus status) {
        System.out.println("[Order] 状态变更: " + this.status + " -> " + status);
        this.status = status;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }
}